package http;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
  
public class BatchDownLoad {  
  
    public static void main(String args[]) throws Exception {  
//    	ExecutorService executorService = Executors.newFixedThreadPool(10);
//    	
//    	String path = "C:\\Users\\admin\\Desktop\\aaa.txt";
//    	
    	final String toPath = "C:\\Users\\admin\\Desktop\\articles\\";
//    	
//    	new File(toPath).mkdirs();
//    	
//    	BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
//        
//    	String line = null;
//    	
//    	while((line=bufferedReader.readLine()) != null){
//    		final String url = line.substring(line.indexOf("http")).trim();
//    		final String name = line.substring(line.lastIndexOf("/")+1).trim();
//    		
//    		
//    		if(new File(toPath+name).exists()){
//    			continue;
//    		}
//    		executorService.execute(new Runnable() {
//				@Override
//				public void run() {
//					try {
//						downloadFromUrl(url, toPath+name);
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
//				}
//			});
//    		
//    		Thread.sleep(300);
//    	}
//    	
//    	bufferedReader.close();
    	
    	
    	
    	downloadFromUrl("http://www.goldenlampstand.org/glb/readglb.php?GLID=12203", toPath+"readglb.html");
    	
    }  
    
    
    private static void downloadFromUrl(String urlString,String target) throws Exception{
    	URL url = new URL(urlString);  
        URLConnection conn = url.openConnection();  
        InputStream is = conn.getInputStream();  
        ByteArrayOutputStream out = new ByteArrayOutputStream();  
        byte[] b = new byte[1024];  
        int len = 0;  
        while ((len = is.read(b)) != -1) {  
            out.write(b, 0, len);  
        }  
        String result = out.toString("big5").replace("charset=big5", "charset=UTF-8");  
        out.close();  
        is.close();  
        
        FileWriter fileWriter = new FileWriter(target);
        fileWriter.write(result);
        fileWriter.flush();
        fileWriter.close();
    }
  
}